package com.dunshan.prcexp.util;

import com.alibaba.fastjson.JSON;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang3.StringUtils;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author liwen406
 * @program: dunshan-pef
 * @description:
 * @date 2022-08-25 10:31:17
 */
public class TextUtil {

    public TextUtil() {
    }

    public static List<String> rex(String text, String rex) {
        List<String> variable = new ArrayList();
        Pattern pattern = Pattern.compile(rex);
        Matcher matcher = pattern.matcher(text);

        while (matcher.find()) {
            variable.add(matcher.group());
        }

        return variable;
    }

    public static String lineToHump(String str) {
        Pattern linePattern = Pattern.compile("_(\\w)");
        str = str.toLowerCase();
        Matcher matcher = linePattern.matcher(str);
        StringBuffer sb = new StringBuffer();

        while (matcher.find()) {
            matcher.appendReplacement(sb, matcher.group(1).toUpperCase());
        }

        matcher.appendTail(sb);
        return sb.toString();
    }

    public static String replaceVariable(String oldStr) {
        String retStr = "";
        if (!StringUtils.isEmpty(oldStr)) {
            if (ThreadLocalUtil.getVariableData() != null) {
                retStr = TokenParserUtil.parse(oldStr);
            } else {
                retStr = oldStr;
            }
        }

        return retStr;
    }

    public static String getVariableValue(String key) {
        String retStr = key;
        if (!StringUtils.isEmpty(key) && key.startsWith("${") && key.endsWith("}") && ThreadLocalUtil.getVariableData() != null) {
            retStr = ThreadLocalUtil.getVariableData().get(key.replace("${", "").replace("}", "")).toString();
        }

        return retStr;
    }

    public static Object replaceVariable(Object oldObj) {
        Object retObj = null;
        if (oldObj != null) {
            if (ThreadLocalUtil.getVariableData() != null) {
                retObj = JSON.parse(TokenParserUtil.parse(oldObj.toString()));
            } else {
                retObj = oldObj;
            }
        }

        return retObj;
    }

    public static String exceptionToString(Exception e) {
        StringWriter sw = new StringWriter();
        e.printStackTrace(new PrintWriter(sw));
        return sw.toString();
    }

    public static String urlEncode(String plain) {
        try {
            return URLEncoder.encode(plain, "UTF-8");
        } catch (Exception var2) {
            var2.printStackTrace();
            return plain;
        }
    }

    public static String urlDecode(String plain) {
        try {
            return URLDecoder.decode(plain, "UTF-8");
        } catch (Exception var2) {
            var2.printStackTrace();
            return plain;
        }
    }

    public static boolean isChinese(String var) {
        char[] var1 = var.toCharArray();
        int var2 = var1.length;

        for (int var3 = 0; var3 < var2; ++var3) {
            char c = var1[var3];
            if (isChinese(c)) {
                return true;
            }
        }

        return false;
    }

    public static String base64Encode(String plain) {
        try {
            return new String(Base64.encodeBase64(plain.getBytes()));
        } catch (Exception var2) {
            var2.printStackTrace();
            return plain;
        }
    }

    public static String base64Decode(String cipher) {
        try {
            return new String(Base64.decodeBase64(cipher), StandardCharsets.UTF_8);
        } catch (Exception var2) {
            var2.printStackTrace();
            return cipher;
        }
    }

    private static boolean isChinese(char c) {
        Character.UnicodeBlock ub = Character.UnicodeBlock.of(c);
        return ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS || ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_B || ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION || ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS || ub == Character.UnicodeBlock.GENERAL_PUNCTUATION;
    }
}
